﻿using System;
using System.Configuration;

namespace DyllonSoft.DBManager
{
    /// <summary>
    /// 数据库连接信息类
    /// </summary>
    public class DBConnectionInfo
    {
        #region 私有字段
        /// <summary>
        /// 数据库连接字符串
        /// </summary>
        private string _connectionString;
        /// <summary>
        /// 数据库连接类型
        /// </summary>
        private DBConnectionType _connectionType;
        #endregion

        #region 只读属性
        /// <summary>
        /// 数据库连接字符串
        /// </summary>
        public string ConnectionString
        {
            get
            {
                return _connectionString;
            }
        }
        /// <summary>
        /// 数据库连接类型
        /// </summary>
        public DBConnectionType ConnectionType
        {
            get
            {
                return _connectionType;
            }
        }
        #endregion

        #region 构造方法
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="connStr">数据库连接字符串</param>
        /// <param name="connType">数据库连接类型</param>
        public DBConnectionInfo(string connStr, DBConnectionType connType)
        {
            _connectionString = connStr;
            _connectionType = connType;
        }
        #endregion

        #region 静态方法
        /// <summary>
        /// 创建数据库连接信息对象
        /// </summary>
        /// <param name="connStr">数据库连接字符串</param>
        /// <param name="connType">数据库连接类型</param>
        /// <returns>返回一个数据库连接信息对象</returns>
        public static DBConnectionInfo Create(string connStr, DBConnectionType connType)
        {
            return Activator.CreateInstance(typeof(DBConnectionInfo), connStr, connType) as DBConnectionInfo;
        }

        /// <summary>
        /// 构造方法
        /// 从app.config文件或web.config文件中读取配置信息
        /// 相关设置项:
        ///     appSettings:
        ///         DataBaseType    -  数据库类型(可选值: MSSQL, ACCESS, MYSQL, ORACLE, OLEDB, ODBC)
        ///         EncryptConnStr  -  是否加密数据库连接字符串(可选值: 1 - 是, 0 - 否)
        ///     connectionStrings
        ///         MsSqlConnStr    -  MSSQL数据库连接字符串, 根据DataBaseType确定是否添加该设置项
        ///         AccessConnStr   -  ACCESS数据库连接字符串, 根据DataBaseType确定是否添加该设置项
        ///         MySqlConnStr    -  MYSQL数据库连接字符串, 根据DataBaseType确定是否添加该设置项
        ///         OracleConnStr   -  ORACLE数据库连接字符串, 根据DataBaseType确定是否添加该设置项
        ///         OleDbConnStr    -  通过OLEDB访问的数据库连接字符串, 根据DataBaseType确定是否添加该设置项
        ///         OdbcConnStr     -  通过ODBC访问的数据库连接字符串, 根据DataBaseType确定是否添加该设置项
        /// 注意:
        ///     如果应用程序根目录下找不到app.config文件, 需手工添加改文件
        ///     文件内容如下:
        ///     <![CDATA[
        ///     <?xml version="1.0" encoding="utf-8" ?>
        ///     <configuration>
        ///         <appSettings>
        ///             <add key="DataBaseType" value="ACCESS" />
        ///             <add key="EncryptConnStr" value="0" />
        ///         </appSettings>
        ///         <connectionStrings>
        ///             <add name="MsSqlConnStr" connectionString="Data Source=SqlServerAddress; Initial Catalog=DataBaseName; User ID=UserName; pwd=UserPassword" providerName="System.Data.SqlClient" />
        ///             <add name="AccessConnStr" connectionString="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=DataBaseFilePath" providerName="System.Data.OleDb"/>
        ///             <add name="MySqlConnStr" connectionString="Server=ServerAddress; Port=PortNum; Database=DataBaseName; Uid=UserName;Pwd=UserPassword" providerName="MySql.Data.MySqlClient"/>
        ///             <add name="OracleConnStr" connectionString="Data Source=DataBaseName; User Id=UserName; Password=UserPassword; Integrated Security=no" providerName="System.Data.OracleClient"/>
        ///             <add name="OleDbConnStr" connectionString="Provider=ProviderName; Data Source=MyOracleDB; User Id=UserName; Password=asdasd;" providerName="System.Data.OleDb"/>
        ///             <add name="OdbcConnStr" connectionString="driver={Driver Name}; Database=DataBaseName; hostname=ServerName; port=PortNum; protocol=TCPIP; uid=UserName; pwd=UserPassword" providerName="System.Data.Odbc"/>
        ///         </connectionStrings>
        ///     </configuration>
        ///     ]]>
        /// </summary>
        public static DBConnectionInfo GetDBConnectionInfo()
        {
            string connStr = string.Empty;
            DBConnectionType connType = DBConnectionType.SqlServer;
            switch (ConfigurationManager.AppSettings["DataBaseType"].ToUpper())
            {
                case "MSSQL":
                    connStr = ConfigurationManager.ConnectionStrings["MsSqlConnStr"].ConnectionString;
                    connType = DBConnectionType.SqlServer;
                    break;
                case "MYSQL":
                    connStr = ConfigurationManager.ConnectionStrings["MySqlConnStr"].ConnectionString;
                    connType = DBConnectionType.MySql;
                    break;
                case "ORACLE":
                    connStr = ConfigurationManager.ConnectionStrings["OracleConnStr"].ConnectionString;
                    connType = DBConnectionType.Oracle;
                    break;
                case "OLEDB":
                    connStr = ConfigurationManager.ConnectionStrings["OleDbConnStr"].ConnectionString;
                    connType = DBConnectionType.OleDb;
                    break;
                case "ACCESS":
                    connStr = ConfigurationManager.ConnectionStrings["AccessConnStr"].ConnectionString;
                    connType = DBConnectionType.OleDb;
                    break;
                case "ODBC":
                    connStr = ConfigurationManager.ConnectionStrings["OdbcConnStr"].ConnectionString;
                    connType = DBConnectionType.Odbc;
                    break;
            }
            return Activator.CreateInstance(typeof(DBConnectionInfo), connStr, connType) as DBConnectionInfo;
        }
        #endregion
    }
}
